var cf = require("../../../config.js");
var util = require("../../../utils/util.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
var baseHandle = require("../../template/baseHandle.js");
//获取应用实例
var app = getApp();
Page(Object.assign({}, baseHandle, {

  /**
   * 页面的初始数据
   */
  data: {
    extConfig: wx.getExtConfigSync ? wx.getExtConfigSync() : {},
    app: app,
    staticResPath: cf.config.staticResPath,
    userImagePath: cf.config.userImagePath,
    array: ['全部产品', '已发布', '已下架'],
    index: 0,
    isLoading:false,
    curPage:1,
    isShow:false,
    isBottom:false,
    isUp:true,
    goods:[],
    curIdx:-1,
    isEmployeer:false,
    Id:"",
    needReload:false,//在添加自定义产品回显的时候需要用到
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
    this.getGoods();
  },
  operateGoods:function (e){
    var ctx = this;
    var goodsId = e.currentTarget.id;
    var emId = e.currentTarget.dataset.id;
    var isUp = e.currentTarget.dataset.up;
    var idx = e.currentTarget.dataset.idx;
      this.setData({
        isShow:true,
        Id: goodsId,
        isUp:isUp,
        curIdx: idx,
        isRecommended: ctx.data.goods[idx].isRecommended
      })
      if(emId){
        this.setData({
          isEmployeer: true
        })
      }else{
        this.setData({
          isEmployeer: false
        })
      }
  },
  cancel:function (){
    this.setData({
      isShow: false
    })
  },
  //编辑产品
  editGoods:function(){
    var ctx = this;
    wx.navigateTo({
      url: '/pages/sales/my_shop/add_goods?goodsId=' + ctx.data.Id +'&action=edit',
    })
  },
  linkToAdd:function (){
    wx.navigateTo({
      url: '/pages/sales/my_shop/add_goods',
    })
  },
  //上架
  upGoods: function () {
    var ctx = this;
    wx.request({
      url: cf.config.pageDomain + '/applet/mobile/visitcard/goods/upGoods',
      data: {
        cusmallToken: cusmallToken,
        goodsId: ctx.data.Id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
        if (data && data.ret == 0) {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: "上架成功"
          })
          ctx.setData({
            isShow:false
          })
          ctx.getGoods();
        } else {

        }
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },
  //下架
  downGoods: function () {
    var ctx = this;
    wx.request({
      url: cf.config.pageDomain + '/applet/mobile/visitcard/goods/downGoods',
      data: {
        cusmallToken: cusmallToken,
        goodsId:ctx.data.Id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
        if (data && data.ret == 0) {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: "下架成功"
          })
          ctx.setData({
            isShow: false
          })
          ctx.getGoods();
        } else {

        }
      },
      complete: function () {
        wx.hideLoading();
      }
    })

  },
  //删除
  deleteGoods: function () {
    var ctx = this;
    wx.showModal({
      title: '删除商品',
      content: '确定执行此操作？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: cf.config.pageDomain + '/applet/mobile/visitcard/goods/delGoods',
            data: {
              cusmallToken: cusmallToken,
              goodsId: ctx.data.Id
            },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              var data = res.data;
              if (data && data.ret == 0) {
                wx.showModal({
                  title: '提示',
                  showCancel: false,
                  content: "删除成功"
                })
                ctx.that.getGoods()
                ctx.setData({
                  isShow: false
                })
              } else {

              }
            },
            complete: function () {
              wx.hideLoading();
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    app.getUserInfo(this, options, function (userInfo, res) {
      cusmallToken = wx.getStorageSync('cusmallToken');
      let employeeMsg = wx.getStorageSync('employee');
      that.setData({
        employeeMsg: employeeMsg,
        eId:employeeMsg.id,
      });
      that.getGoods();
    });
    
  },
  getGoods: function () {
    var ctx = this;
    if (ctx.data.isLoading || ctx.data.isBottom) {
      return;
    }
    wx.showLoading({
      title: '加载中',
    });
    wx.request({
      url: cf.config.pageDomain + '/applet/mobile/visitcard/goods/queryGoodsByEid',
      data: {
        cusmallToken: cusmallToken,
        eid: ctx.data.eId,
        searchType: ctx.data.index,
        start: (ctx.data.curPage - 1) * 10,
        limit: 20
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
        var goodslist = data.model.records;
        if (data && data.ret == 0) {
          for (var i = 0; i < data.model.records.length; i++) {
            data.model.records[i].pics = data.model.records[i].pics.split(",");
            var date = new Date(data.model.records[i].createTime);
            data.model.records[i].createTime = util.formatDate(date);
          }
          ctx.setData({
            goods:goodslist
          })
          // if (data.model.total == ctx.data.goodlists.length) {
          //   ctx.setData({
          //     isBottom: true
          //   })
          // }
        } else {

        }
      },
      complete: function () {
        wx.hideLoading();
      }
    })

  },
  /**
   * 设置推荐
   */
  setRecommand(){
    var ctx = this;
    console.log("setRecommand succ")
    wx.request({
      url: cf.config.pageDomain + '/applet/mobile/visitcard/goods/updateGoodsRecommended',
      data: {
        cusmallToken: cusmallToken,
        goodsId: ctx.data.Id,
        eid: ctx.data.employeeMsg.id,
        isRecommended: !ctx.data.isRecommended
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
        if (data && data.ret == 0) {
          wx.showToast({
            title: ctx.data.isRecommended ? "已取消推荐" : "推荐成功",
            icon:"none"
          });
          ctx.setData({
            isShow: false,
            ["goods[" + ctx.data.curIdx + "].isRecommended"]: !ctx.data.isRecommended
          });

        } else {
          wx.showToast({
            title: data.msg,
            icon: "none"
          });
        }
      },
      complete: function () {
       
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.needReload){
      
      this.getGoods();
    }
    this.setData({
      needReload: true,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // let ctx = this;
    // if (ctx.data.isLoading) {
    //   return;
    // }
    // ctx.setData({
    //   goods: [],
    //   isLoading: false,
    //   curPage: 1,
    //   isBottom:false
     
    // });
    // ctx.getGoods();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // let ctx = this;
    // if (ctx.data.isLoading) {
    //   return;
    // }
    // ctx.setData({ curPage: ctx.data.curPage + 1 });
    // ctx.getGoods();
  },

  /**
   * 用户点击右上角
   */
  onShareAppMessage: function () {

  }
}))